<!-- 发货明细 -->
<template>
  <el-dialog
    :title="modalData.title"
    :visible.sync="dialog.show"
    center
    width="600px"
    custom-class="mydialog"
    append-to-body
    @close="handleClose"
  >
    <div class="dialog-content">
      <el-table
        :data="modalData.selectPurchaseItemList"
        border
        style="width: 100%"
        class="dialog-table"
      >
        <el-table-column prop="skuName" label="SKU名称" width="120"/>
        <el-table-column prop="pendingReceiptAmount" label="待收货数量"/>
        <el-table-column label="本次收货数量">
          <template slot-scope="scope">
            <el-input v-model="scope.row.collectAmountPlus"/>
          </template>
        </el-table-column>
      </el-table>
      <div class="text-center mt-30">
        <el-button class="greenbt" type="primary" @click="confirmReceipt">确认收货</el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import { post } from '@/api/utils'
import { message } from '@/utils/utils'

export default {
  props: {
    modalStaticData: {
      type: Object,
      default: () => {}
    },
    // 父组件调用时传进来的参数
    modalData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      dialog: {
        title: '',
        show: true
      },
      tableData: [
        {
          id: '12987122',
          order: '23',
          amount1: '234',
          amount2: '3.2',
          amount3: 10
        },
        {
          id: '12987123',
          order: '23',
          amount1: '165',
          amount2: '4.43',
          amount3: 12
        }
      ]
    }
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },
    confirmReceipt() {
      if (
        this.modalData.selectPurchaseItemList &&
        this.modalData.selectPurchaseItemList.length > 0
      ) {
        var selectPurchaseItemList = this.modalData.selectPurchaseItemList

        var isReceipts = 0
        for (var i = 0; i < selectPurchaseItemList.length; i++) {
          var purchaseItem = selectPurchaseItemList[i]
          if (purchaseItem && purchaseItem.pendingReceiptAmount !== null) {
            if (purchaseItem.collectAmountPlus !== null && purchaseItem.collectAmountPlus > 0) {
              if (
                purchaseItem.collectAmountPlus >
                purchaseItem.pendingReceiptAmount
              ) {
                message(this, '本次收货数量不能大于可收货数量')

                return false
              }
              isReceipts++
            } else {
              message(this, '请输入第' + (i + 1) + '行收货数量')
              return false
            }
          }
        }

        if (isReceipts !== 0) {
          var data = selectPurchaseItemList.map(purchaseItem => {
            return {
              purchaseItemId: purchaseItem.purchaseItemId,
              collectAmountPlus: purchaseItem.collectAmountPlus
            }
          })
          post('/wxh-cloud-rest/rest/', '/pas/purchaseItem/receipt', data)
            .then(response => {
              if (response.data.status === 'success') {
                console.log(response)
                message(this, '收货成功', 'success')
                this.modalStaticData.closeChildAndRefresh()
              } else if (response.data.status === 'faild') {
                this.$message({
                  message: response.data.messge,
                  type: 'warning'
                })
              }
            })
            .catch(error => {
              console.log(error)
              this.$message({
                message: '数据加载失败',
                type: 'warning'
              })
            })
        } else {
          message(this, '请至少填写一项进行收货')
        }
      }
    }
  }
}
</script>
